package org.qqcui.designpattern.patterns.command;

/**
 * 远程控制的实现类
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022/5/6 11:15
 **/
public class RemoteController {

    /**
     * 一组开启命令
     */
    private Command[] onCommands;

    /**
     * 一组关闭命令
     */
    private Command[] offCommands;

    /**
     * 撤销命令
     */
    private Command undoCommand;

    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
        undoCommand = new NoCommand();
    }

    /**
     * 设置下标为index的开/关命令
     * @param index 下标位置
     * @param onCommand 开启命令
     * @param offCommand 关闭命令
     */
    public void setCommands(int index, Command onCommand, Command offCommand) {
        onCommands[index] = onCommand;
        offCommands[index] = offCommand;
    }

    /**
     * 按下下标为index的开始按钮
     * @param index 下标位置
     */
    public void onButtonWasPushed(int index) {
        onCommands[index].execute();
        undoCommand = onCommands[index];
    }

    /**
     * 按下下标为index的关闭按钮
     * @param index 下标位置
     */
    public void offButtonWasPushed(int index) {
        offCommands[index].execute();
        undoCommand = offCommands[index];
    }

    public void undoButtonWasPushed() {
        undoCommand.undo();
    }
}
